// pages/index/swiper/swiper.js
import {list} from '../../../../api/ad'
const util = require('../../../../utils/util')
let app = getApp();
Component({
  
      
 
  // 以下是旧式的定义方式，可以保持对 <2.2.3 版本基础库的兼容
  attached: function() {

    // 在组件实例进入页面节点树时执行
   
  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isSwiperShow:{
      type:Boolean,
    },
    info:{
      type:Object,
    }
  },
  lifetimes:{
    attached(){
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
        this.setData({
          tagIds:this.data.tagIds
        })
      
        this.loadAd()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    tagIds:[],
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    photoList:[],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    previousMargin: 0,
    color:[],
    c:'',
    nextMargin: 0,
    pageNum: 0,
    pageSize: 10,

  },
  
  /**
   * 组件的方法列表
   */
  created(){
   
  },
  methods: {
    test(){
      util.token(app.globalData.token)
    },
    toLink(e) {
            
      if(e.currentTarget.dataset.type.hasOwnProperty('typeId') && !util.isNull(e.currentTarget.dataset.type.typeId)){
      if (e.currentTarget.dataset.type.type === 0) {
        wx.navigateTo({
          url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.type.typeId
        })
      } else if (e.currentTarget.dataset.type.type === 9) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.type.typeId
        })
      }else if (e.currentTarget.dataset.type.type === 2) {
        wx.navigateTo({
          url:'/pages/member/detail/detail?id='+e.currentTarget.dataset.type.typeId,
        })
      } else if (e.currentTarget.dataset.type.type === 1) {
        wx.navigateTo({
          url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.type.typeId
        })
      }   
    }
    },
    searchClick(){
      wx.navigateTo({
        url: '/pages/goods/search/index'
     })
    },
    changeBackground(e){
       this.setData({
        c:this.data.color[e.detail.current]
       })
    },
    detailsClick(){
      wx.navigateTo({
        url: '/pages/goods/product/details',
      })
    },

    loadAd(){
      var _this = this
      list({
        mchId:app.globalData.mchId,
        tagIds:this.data.tagIds
      }).then(res=>{
        res.data.data.forEach(element => {
          element.backgroundColor="linear-gradient(180deg,"+element.backgroundColor+", rgba(248, 248, 248, 1))"
          _this.data.color.push(element.backgroundColor)
          element.thumbnail=util.thumbnail(element.thumbnail,750,510)
          this.data.c=this.data.color[0]
         });
      _this.data.photoList = res.data.data
      this.setData({  
        color:this.data.color,
        photoList:_this.data.photoList,
        c:this.data.c,
        statusBarHeight: app.globalData.statusBarHeight,
      });
      });
    }
  }
})
